﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using WeatherSystem.Application.ContentAdapter.Base;
using WeatherSystem.Application.ContentAdapter.TextAdapter;
using WeatherSystem.Application.MsgHandler;
using WeatherSystem.Data.Response;

namespace WeatherSystem.Application.ContentAdapter
{
    class TextManager : AdapterBase, Adapter
    {
        public override Type AdaptType => typeof(TextMessage);
        private static List<Adapter> TextAdapters = new List<Adapter>();
        private static Default DefaultAdapter = new Default();
        public static void AddAdapter(TextAdapterBase adapter)
        {
            foreach (var adpt in TextAdapters)
            {
                var a = adpt as TextAdapterBase;
                if (adapter.KeyWord.IndexOf(a.KeyWord) != -1 || a.KeyWord.IndexOf(adapter.KeyWord) != -1) return;
            }
            TextAdapters.Add(adapter);
        }
        public ResponseMessageBase Handle(Message message)
        {
            foreach(var adapter in TextAdapters)
            {
                var adapter_base = adapter as TextAdapterBase;
                if (adapter_base.Adapt(message as TextMessage))
                {
                    return adapter.Handle(message);
                }
            }
            return DefaultAdapter.Handle(message);
        }
    }

}
